package com.levi.area;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@Component
public class ProvinceCach {
    public static String provinceJsonstr;
    @Autowired
    private ResourceLoader resourceLoader;

    @PostConstruct
    public void setProvinceList(){
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        StringBuilder jsonStr = null;
        try {
            Resource resource = resourceLoader.getResource("classpath:province.json");
            is = resource.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            jsonStr = new StringBuilder();
            String data;
            while((data = br.readLine()) != null) {
                jsonStr.append(data);
            }
            br.close();
            isr.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String jsonStrBody =jsonStr.toString();
        provinceJsonstr = jsonStrBody;
        System.out.println("初始化json文件成功～～");
    }

    public static String getProvinceJsonstr(){
        return provinceJsonstr;
    }
}
